//
//  ListInfoGiftView.m
//  tcpoctest
//
//  Created by gump on 5/27/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "ListInfoGiftView.h"

#import "GiftInfoDataSource.h"
#import "GiftListDataSource.h"

#import "GiftItemView.h"

#import "GiftInfoDataInfo.h"

#import "chatDataSource.h"
#import "chatuserdb.h"

#import "sendGiftListLength.h"
#import "sendPersonReadCoin.h"
#import "sendPotocol.h"

#import <QuartzCore/QuartzCore.h>

@implementation ListInfoGiftView

-(id)init
{
    self = [super init];
    
    if (self != nil) {
        LIGiftViewDic = [[NSMutableDictionary alloc] init];
    }
    
    return self;
}

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"选择礼物";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    
    LICoinLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TOPBAR_HIGHT, WINDOW_WIDTH, TOPBAR_HIGHT)];
    LICoinLabel.textAlignment = UITextAlignmentCenter;
    
    float fprice = [[DataControl shareControl].personDS.uinfo.strcoin floatValue];
    int iprice = fprice / 10000;
    if (iprice > 0) {
        LICoinLabel.text = [NSString stringWithFormat:@"我的金币: %d万",iprice];
    }
    else
    {
       LICoinLabel.text = [NSString stringWithFormat:@"我的金币: %@",[DataControl shareControl].personDS.uinfo.strcoin]; 
    }
    
    [self.view addSubview:LICoinLabel];
    
    LIScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,
                                                                  TOPBAR_HIGHT * 2,
                                                                  WINDOW_WIDTH,
                                                                  WINDOW_HIGTH - TOPBAR_HIGHT * 2)];
    
    [self.view addSubview:LIScrollView];
    
    UIBarButtonItem *modifyItem = [self createBarButtonItemWithTitle:@"分类" target:self action:@selector(nextButtonPressed)];
    self.navItem.rightBarButtonItem = modifyItem;
    
    mGiftTypeTable = [[UITableView alloc] initWithFrame:CGRectMake(WINDOW_WIDTH,
                                                                   TOPBAR_HIGHT,
                                                                   WINDOW_WIDTH - 160,
                                                                   WINDOW_HIGTH - TOPBAR_HIGHT)
                                                  style:UITableViewStylePlain];
    
    mGiftTypeTable.backgroundColor=
    [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:0.5];
    mGiftTypeTable.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    mGiftTypeTable.delegate =self;
    mGiftTypeTable.dataSource =self;
    [self.view addSubview:mGiftTypeTable];
    
}

#pragma mark -private fun-

-(void)backButtonPressed
{
    [self HideTypeList];
    
    [[ViewManager sharedViewManager] backView:self];
}

-(void)nextButtonPressed
{
    if (mGiftTypeTable.frame.origin.x >= WINDOW_WIDTH) {
        [self ShowTypeList];
    }
    else
    {
        [self HideTypeList];
    }
}

-(void)ShowTypeList
{
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    
    mGiftTypeTable.frame =  CGRectMake(160,
                                       TOPBAR_HIGHT,
                                       WINDOW_WIDTH - 160,
                                       WINDOW_HIGTH - TOPBAR_HIGHT);
    
    [[netManager sharedNetManager].sendp SendMainIndex:ID_MI_GIFT
                                       WithSecondIndex:GI_GET];
    
    [DataControl shareControl].giftInfoDS.strSenderMail =
    [DataControl shareControl].currentFI.mail;
    
    [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"正在获取礼物列表"
                                                      withView:self.view];
    
    iTimeCount = 0;
    iGameState = VGS_START;

}

-(void)SelectGiftRequestBakc
{
    [[ViewManager sharedViewManager] dismissProgressHUD];
    
    [mGiftTypeTable reloadData];
    
    iTimeCount = 0;
    iGameState = VGS_NULL;
}

-(void)UICustomTick
{
    switch (iGameState) {
        case VGS_START:
        {
            if (iTimeCount < 200) {
                iTimeCount++;
            }
            else
            {
                iTimeCount = 0;
                iGameState = VGS_NULL;
                
                [[ViewManager sharedViewManager] dismissProgressHUD];
            }
        }
            break;
            
        default:
            break;
    }
}

-(void)HideTypeList
{
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    
    mGiftTypeTable.frame =  CGRectMake(WINDOW_WIDTH,
                                       TOPBAR_HIGHT,
                                       WINDOW_WIDTH - 160,
                                       WINDOW_HIGTH - TOPBAR_HIGHT);
}

-(void)viewWillShow
{
    NSArray *gvarr = [LIGiftViewDic allValues];
    for (GiftItemView *giv in gvarr) {
        if (giv != nil) {
            [giv removeFromSuperview];
        }
    }
    
    [LIGiftViewDic removeAllObjects];
    
    
    [self refreshView];
    
}

-(void)refreshView
{
    int icount = [[DataControl shareControl].giftInfoDS GetShowCount];
    CGSize newSize = CGSizeMake(WINDOW_WIDTH, (icount/4 + 1) * GIFT_VIEW_HIGHT + 20);
    [LIScrollView setContentSize:newSize];
    
    NSMutableArray *showarr =  [NSArray arrayWithArray:
                                [[DataControl shareControl].giftInfoDS GetShowArr]];
    
    if (showarr != nil) {
        for (int i = 0; i < showarr.count; ++i){
            NSString *giftid = [showarr objectAtIndex:i];
            GiftItemView *giv = [LIGiftViewDic objectForKey:giftid];
            if (giv == nil) {
                giv = [self creatGiftItemViewByGiftID:giftid];
                
                if (giv != nil) {
                    [LIGiftViewDic setObject:giv forKey:giftid];
                    
                    [self addGiftView:giv withDicCount:i];
                }
                
            }
            else
            {
                [giv ReSetImageByID:giftid];
                [self ResetFramebyCount:i withItemView:giv];
            }
        }
    }
    
    float fprice = [[DataControl shareControl].personDS.uinfo.strcoin floatValue];
    int iprice = fprice / 10000;
    if (iprice > 0) {
        LICoinLabel.text = [NSString stringWithFormat:@"我的金币: %d万",iprice];
    }
    else
    {
        LICoinLabel.text = [NSString stringWithFormat:@"我的金币: %@",[DataControl shareControl].personDS.uinfo.strcoin];
    }
}

-(void)ResetFramebyCount:(int)icount withItemView:(GiftItemView *)giv
{
    int row = icount  % 4;
    
    int line = icount / 4;
    
    giv.frame = CGRectMake(GIFT_VIEW_WIDTH * row,
                           GIFT_VIEW_HIGHT * line,
                           GIFT_VIEW_WIDTH,
                           GIFT_VIEW_HIGHT);
}

-(GiftItemView *)creatGiftItemViewByGiftID:(NSString *)iid
{
    GiftItemView *giv = [[GiftItemView alloc] initWithFrame:
    CGRectMake(0, 0, GIFT_VIEW_WIDTH, GIFT_VIEW_HIGHT)];
    
    giv.ginfo = [[DataControl shareControl].giftInfoDS getGiftInfoByID:iid];
    
    [giv initUI];
    
    GiftInfoDataInfo *ginfo = [[DataControl shareControl].giftInfoDS getGiftInfoByID:iid];
    
    if (ginfo != nil) {
        
        [giv ReSetImageByID:iid];
        
        int iprice = ginfo.giftprice / 10000;
        if (iprice > 0) {
            giv.giftLabel.text = [NSString stringWithFormat:@"价格:%d万",iprice];
        }
        else
        {
            giv.giftLabel.text = [NSString stringWithFormat:@"价格:%d",ginfo.giftprice];
        }
        
        giv.giftNameLabel.text = ginfo.strgiftname;
        
        if (ginfo.giftIsMusic == 2) {
            giv.giftLabel.textColor = [UIColor redColor];
            giv.giftNameLabel.textColor = [UIColor redColor];
        }
    }
    
    return giv;
}

-(void)addGiftView:(GiftItemView *)giv withDicCount:(int)icount
{
    int row = icount % 4;
    
    int line = icount / 4;
    
    giv.frame = CGRectMake(GIFT_VIEW_WIDTH * row,
                           GIFT_VIEW_HIGHT * line,
                           GIFT_VIEW_WIDTH,
                           GIFT_VIEW_HIGHT);
    
    [LIScrollView addSubview:giv];
}

-(void)PopDialog
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                    message:@""
                                                   delegate:self
                                          cancelButtonTitle:@"继续送礼"
                                          otherButtonTitles:@"和他聊天",nil];
    [alert show];
}

#pragma mark -alertview delegate-
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        if ([DataControl shareControl].currentFI == nil) {
            return;
        }
        
        NSMutableArray *chatarr = [[DataControl shareControl].chatDS GetArrByKey:
                                   [DataControl shareControl].currentFI.mail];
        if (chatarr == nil)
        {
            chatarr = [[NSMutableArray alloc] init];
            [[DataControl shareControl].chatDS SetChatDicArr:chatarr withKey:
             [DataControl shareControl].currentFI.mail];
            [[DataBaseCtrol shareControl].chatuserdatabase insertChatDic:[DataControl shareControl].currentFI.mail
                                                             withIsGroup:false];
            
            
        }
        
        [DataControl shareControl].chatDS.currentChatArr = chatarr;
        
        [[ViewManager sharedViewManager] showView:VI_CHAT
                                 withIsRemoveCurV:false
                                    withDirection:VD_RIGHT];

    }
}

#pragma mark - tableView DataSource -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0)
    {
        if ([DataControl shareControl].giftlistDS != nil) {
            
            return [[DataControl shareControl].giftlistDS GetShowArrCount];
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section=[indexPath section];
    NSUInteger row = [indexPath row];
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
		cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    cell.imageView.image = nil;
    cell.textLabel.text = @"";
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    switch (section)
    {
        case 0:
        {
            if ([DataControl shareControl].giftlistDS != nil &&
                row < [[DataControl shareControl].giftlistDS GetShowArrCount]) {
                
                cell.textLabel.text = [[DataControl shareControl].giftlistDS GetNameByRow:row];
                
                NSString *strpath = [[DataControl shareControl].giftlistDS searchPathByRow:row];
                
                if (strpath == nil || [strpath isEqualToString:@""]) {
                    cell.imageView.image = [UIImage imageNamed:@"gift_default.png"];
                }
                else
                {
                    cell.imageView.image = [UIImage imageWithContentsOfFile:strpath];
                }
                
            }
            
            
        }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            break;
        default:
            break;
    }
    
    return cell;
}

#pragma mark -table delegate-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch ([indexPath section]) {
        case 0:
        {
            if ([DataControl shareControl].OnlinePeoDS != nil)
            {
                int row = [indexPath row];
                if (row >=0 && row < [[DataControl shareControl].giftlistDS GetShowArrCount])
                {
                    int iid = [[DataControl shareControl].giftlistDS GetGiftIDByRow:row];
                    
                    [[DataControl shareControl].giftInfoDS SetShowArrByListID:iid];
                    
                    [[netManager sharedNetManager].sendgiftLL sendGiftLL:iid];
                    
                    [self HideTypeList];
                    [self viewWillShow];
                }
                
            }
        }
            break;
            
        default:
            break;
    }
}

@end
